#include<iostream>

using std::cin;
using std::cout;
using std::endl;

const int MAXN=1111111;

int fa[MAXN];

int find(int x)
{
	if(x!=fa[x])
		fa[x]=find(fa[x]);
	return fa[x];
}

void merge(int x,int y)
{
	int t=find(x),t2=find(y);
	fa[t]=t2;
}

int main()
{
	while(1)
	{
		int ans=0;
		int n,m,x,y;
		cin>>n>>m;
		if(n==0)
			return 0;
		for(int i=1;i<=n;i++)
			fa[i]=i;
		for(int i=1;i<=m;i++)
		{	
			cin>>x>>y;
			merge(x,y);
		}
		for(int i=1;i<=n;i++)
			if(find(i)==i)
				ans++;
		cout<<ans-1<<endl;
	}
	return 0;
}